package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.pojo.R;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    RedisTemplate redisTemplate;


    @PostMapping("/sendMsg")
    public R<String> sendMsgz(@RequestBody User user, HttpServletRequest req) {
        //获取手机号
        String phone = user.getPhone();
        //判断发过来的手机是否为空或者空字符串
        if (StringUtils.isNotEmpty(phone)) {
            //使用工具类生成6位随机数
            String code = ValidateCodeUtils.generateValidateCode(6).toString();
            log.info("验证码:" + code);
            //将验证码存在session
            //req.getSession().setAttribute(phone, code);

            //使用redis储存验证码                                    设置失效时间
            redisTemplate.opsForValue().set(phone,code,1, TimeUnit.MINUTES);


            return R.success("发送成功");

        }

        return R.error("发送失败");
    }

    @PostMapping("/login")
    public R login(@RequestBody Map map, HttpServletRequest req) {
        //获取前端电话
        String phone = map.get("phone").toString();
        log.info("电话:" + phone);
        //获取验证码
        String code = map.get("code").toString();
        log.info("验证码:" + code);
        //获取保存在session中的id
        //Object codeInSession = req.getSession().getAttribute(phone);
        //使用redis 获取验证码
        Object codeInSession = redisTemplate.opsForValue().get(phone);

        //判断写的验证码与发送的验证码是否相等
        if (codeInSession != null && codeInSession.equals(code)) {
            //登陆成功 需要判断数据库是否存有数据
            //有 直接登录
            //没有 注册并添加数据进数据库
            LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(User::getPhone, phone);
            User one = userService.getOne(lambdaQueryWrapper);
            //数据库中没有数据
            if (one == null) {
                //创建对象并赋值
                one = new User();
                one.setPhone(phone);
                one.setStatus(1);
                //
                userService.save(one);
            }
            req.getSession().setAttribute("one", one.getId());
            //登陆成功后将验证码从redis中清除
            redisTemplate.delete(phone);


            return R.success(one);
        }

        return R.error("登陆失败");
    }
    @PostMapping("/loginout")
    public R<String> loginout(HttpServletRequest req){
        req.getSession().removeAttribute("one");
        return R.success("退出成功");
    }
    //@PostMapping
    //public

}
